
#使用tinkter 画一个登录界面
import tkinter as tk
from tkinter import messagebox
class LoginWindow:
    def __init__(self, root):
        self.root = root
        self.root.title("登录界面")
        self.root.geometry("300x200")

        # 用户名行
        self.username_label = tk.Label(root, text="用户名:")
        self.username_label.grid(row=0, column=0, padx=5, pady=5)
        self.username_entry = tk.Entry(root)
        self.username_entry.grid(row=0, column=1, padx=5, pady=5)

        # 密码行
        self.password_label = tk.Label(root, text="密码:")
        self.password_label.grid(row=1, column=0, padx=5, pady=5)
        self.password_entry = tk.Entry(root, show="*")
        self.password_entry.grid(row=1, column=1, padx=5, pady=5)

        # 登录按钮
        self.login_button = tk.Button(root, text="登录", command=self.login, width=15)
        self.login_button.grid(row=2, column=0, columnspan=2, pady=10)

    def login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()
        if username == "admin" and password == "123456":
            messagebox.showinfo("登录成功", "欢迎登录")
            #销毁当前窗口，打开主界面
            self.root.destroy()
            #创建主界面窗口
            main_window = tk.Tk()
            MainWindow(main_window)
            main_window.mainloop()

        else:
            messagebox.showerror("登录失败", "用户名或密码错误")

class MainWindow:
    def __init__(self, main):
        self.main = main
        self.main.title("主界面")
        self.main.geometry("400x300")
        self.label = tk.Label(main, text="欢迎使用主界面")
        self.label.pack(pady=20)

if __name__ == "__main__":
    root = tk.Tk()
    app = LoginWindow(root)
    root.mainloop()


